<template>
  <div class="field">
    <div class="token">
      <div class="txt">{{ type === "liquidity" ? "Input" : type === "to" ? "To" : "From" }}</div>
      <div class="icon" @click="selectToken">
        <img :src="token.icon" />
        <span>{{ token.symbol }}</span>
        <i class="iconfont icon-arrow-up" />
      </div>
    </div>
    <div class="value">
      <div class="balance">
        <div
          class="g-clickable max"
          @click="setMax"
          v-if="type === 'from' || type === 'liquidity'"
        >
          <span>Max</span>
        </div>
        <div class="txt">Balance: {{ token.balance || 0 }}</div>
      </div>
      <div class="input">
        <imask-input
          :readonly="type === 'to'"
          v-model="amount"
          radix="."
          :mask="Number"
          :unmask="'typed'"
          :scale="10"
          thousandsSeparator =','
          placeholder="enter amount"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { IMaskComponent } from "vue-imask";
export default {
  components: {
    "imask-input": IMaskComponent,
  },
  props: {
    value: {
      type: [Number, String],
    },
    type: {
      type: String,
      default: "from",
    },
    token: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      timeout: null,
      amount: this.value
    };
  },
  watch: {
    value(val) {
      if (+this.amount === +val) return;
      this.amount = val;
    },
    amount(val) {
      if (+this.value === +val) return;
      this.$emit('input', val);
      this.$emit('change', val);
    }
  },
  methods: {
    selectToken() {
      this.$emit("select", this.type);
    },
    setMax() {
      const val = this.token.balance || "0";
      this.$emit("input", val);
      this.$emit("change", val);
    },
    emitInput(e) {
      clearTimeout(this.timeout);
      let val = e.target.value;
      this.timeout = setTimeout(() => {
        val = Math.abs(val || 0)
          .toString()
          .match(RegExp("^\\d*(\\.?\\d{0,8})"))[0];
        this.$emit("input", val);
        this.$emit("change", val);
      }, 500);
    },
  },
};
</script>

<style lang="scss" scoped>
.field {
  background-color: var(--input);
  height: 110px;
  border-radius: 20px;
  padding: 0 25px;
  margin-top: 25px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .token {
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    .icon {
      margin-top: 20px;
      display: flex;
      align-items: center;
      cursor: pointer;

      &:hover {
        span {
          color: var(--color-content);
        }
      }

      img {
        height: 30px;
        border-radius: 15px;
      }

      span {
        font-size: 14px;
        line-height: 1;
        font-family: "Roboto Bold";
        margin: 0 3px 0 7px;
        color: var(--color);
        transition: all 0.3s ease;
      }

      i {
        font-size: 20px;
        transform: rotate(180deg) scale(0.9);
        color: var(--color);
        transition: all 0.3s ease;
      }
    }
  }

  .balance {
    display: flex;
    align-items: center;
    justify-content: flex-end;

    .txt {
      font-size: 14px;
      color: var(--color-content);
    }

    .max {
      cursor: pointer;
      width: 43px;
      height: 22px;
      border-radius: 10px;
      margin-right: 6px;

      display: flex;
      align-items: center;
      justify-content: center;
      background-color: var(--border-color);

      span {
        font-size: 11px;
        line-height: 1;
        font-family: "Roboto Regular";
        color: var(--color-content);
      }
    }
  }

  .input {
    margin-top: 20px;
    input {
      height: 30px;
      background-color: transparent;
      border: none;
      outline: none;
      text-align: right;

      font-size: 22px;
      line-height: 1;
      font-family: "Roboto Black";
      color: var(--color);
    }
  }

  .txt {
    font-size: 14px;
    line-height: 1;
    font-family: "Roboto Regular";
  }
}
</style>
